package com.newsvoice.activity;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import com.google.gson.Gson;

public class LoadImage extends AsyncTask<Void, Void, InputStream> {
	private ImageView imageView;
	private String url;
	public LoadImage(String url, ImageView image) {
		imageView = image;
		this.url = url;
	}

	@Override
	protected InputStream doInBackground(Void... prs) {
		return getInputStreamFromUrl(url);
	}

	@Override
	protected void onPostExecute(InputStream result) {
		Bitmap img = BitmapFactory.decodeStream(result);
		imageView.setImageBitmap(img);

	}

	private InputStream getInputStreamFromUrl(String url) {
		Log.d("load image", url);
		InputStream content = null;

		HttpGet httpGet = new HttpGet(url);
		HttpClient httpclient = new DefaultHttpClient();
		// Execute HTTP Get Request
		HttpResponse response = null;
		try {
			response = httpclient.execute(httpGet);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			Log.d("load image ClientProtocolException", e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("load image IOException", e.getMessage());
		}
		try {
			content = response.getEntity().getContent();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			Log.d("load image IllegalStateException", e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("load image IOException", e.getMessage());
		}
		return content;
	}

}